package com.android.bonn.fast

import android.view.View
import androidx.core.widget.doAfterTextChanged
import com.android.bonn.fast.adapter.CellView
import com.android.bonn.fast.databinding.ItemViewLayout2Binding
import com.android.bonn.fast.databinding.ItemViewLayoutBinding


class FormCellView(view: View, private val formData: AnyAdapterListActivity.FormData? = null) : CellView<FromView>(view) {

  private val binding = ItemViewLayout2Binding.bind(view)

  override fun onBind(data: FromView, position: Int) {

    binding.title.text = data.title
    binding.text.hint = data.hintText


    formData?.apply {
      val value = when (data.id) {
        "1" -> title
        "2" -> number
        "3" -> phone
        "4" -> address
        "5" -> customCat
        else -> ""
      }
      binding.text.setText(value)
    }


    binding.text.doAfterTextChanged {

      formData?.apply {
        when (data.id) {
          "1" -> formData.title = it.toString()
          "2" -> formData.number = it.toString()
          "3" -> formData.phone = it.toString()
          "4" -> formData.address = it.toString()
          "5" -> formData.customCat = it.toString()
          else -> {
          }
        }
      }
    }
  }
}


class StringCellView(view: View, private val formData: AnyAdapterListActivity.FormData? = null) : CellView<String>(view) {

  private val binding = ItemViewLayoutBinding.bind(view)

  override fun onBind(data: String, position: Int) {
    binding.text.text = "String-$data-$position"

  }
}

class IntCellView(view: View) : CellView<Int>(view) {

  private val binding = ItemViewLayoutBinding.bind(view)

  override fun onBind(data: Int, position: Int) {
    binding.text.text = "Int-$data-$position"
  }
}

class BooleanCellView(view: View) : CellView<Boolean>(view) {

  private val binding = ItemViewLayoutBinding.bind(view)

  override fun onBind(data: Boolean, position: Int) {
    binding.text.text = "Boolean-$data-$position"
  }
}


class ExampleCellView(view: View) : CellView<ExampleModel>(view) {

  private val binding = ItemViewLayoutBinding.bind(view)

  init {
    binding.text.setOnClickListener {
      val data = getItem(layoutPosition)
      data.action.invoke()
    }
  }

  override fun onBind(data: ExampleModel, position: Int) {
    binding.text.text = data.title
  }

}